package cn.stylefeng.guns.sys.modular.sysdistrict.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.sys.modular.sysdistrict.entity.SysDistrict;
import cn.stylefeng.guns.sys.modular.sysdistrict.enums.SysDistrictExceptionEnum;
import cn.stylefeng.guns.sys.modular.sysdistrict.mapper.SysDistrictMapper;
import cn.stylefeng.guns.sys.modular.sysdistrict.model.param.SysDistrictParam;
import cn.stylefeng.guns.sys.modular.sysdistrict.model.result.AreaTree;
import cn.stylefeng.guns.sys.modular.sysdistrict.model.result.AreaTreeVO;
import cn.stylefeng.guns.sys.modular.sysdistrict.model.result.CityTreeVO;
import cn.stylefeng.guns.sys.modular.sysdistrict.service.SysDistrictService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * sys_district 服务实现类
 *
 * @author 刘耀
 * @date 2021/03/15 20:13
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysDistrictServiceImpl extends ServiceImpl<SysDistrictMapper, SysDistrict> implements SysDistrictService {

    @Override
    public PageResult<SysDistrict> page(SysDistrictParam sysDistrictParam) {

        // 构造条件
        LambdaQueryWrapper<SysDistrict> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<SysDistrict> list(SysDistrictParam sysDistrictParam) {

        // 构造条件
        LambdaQueryWrapper<SysDistrict> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(sysDistrictParam.getDistrictLevel() != null, SysDistrict::getDistrictLevel, sysDistrictParam.getDistrictLevel());
        queryWrapper.like(StrUtil.isNotBlank(sysDistrictParam.getDistrictName()), SysDistrict::getDistrictName, sysDistrictParam.getDistrictName());
        return this.list(queryWrapper);
    }

    @Override
    public void add(SysDistrictParam sysDistrictParam) {

        // 将dto转为实体
        SysDistrict sysDistrict = new SysDistrict();
        BeanUtil.copyProperties(sysDistrictParam, sysDistrict);

        this.save(sysDistrict);
    }

    @Override
    public void delete(SysDistrictParam sysDistrictParam) {
        this.removeById(sysDistrictParam.getDistrictId());
    }

    @Override
    public void edit(SysDistrictParam sysDistrictParam) {

        // 根据id查询实体
        SysDistrict sysDistrict = this.querySysDistrict(sysDistrictParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(sysDistrictParam, sysDistrict);

        this.updateById(sysDistrict);
    }

    @Override
    public SysDistrict detail(SysDistrictParam sysDistrictParam) {
        return this.querySysDistrict(sysDistrictParam);
    }

    @Override
    public Object allProvince() {
        List<SysDistrict> sysDistricts = baseMapper.selectList(new QueryWrapper<SysDistrict>().lambda().
                eq(SysDistrict::getParentId, 1).orderByAsc(SysDistrict::getSort));
        return sysDistricts;
    }

    @Override
    public Object findCity(Long provinceId) {
        List<SysDistrict> sysDistricts = baseMapper.selectList(new QueryWrapper<SysDistrict>().lambda().
                eq(SysDistrict::getParentId, provinceId).orderByAsc(SysDistrict::getSort));
        return sysDistricts;
    }

    @Override
    public Object findArea(Long cityId) {
        List<SysDistrict> sysDistricts = baseMapper.selectList(new QueryWrapper<SysDistrict>().lambda().
                eq(SysDistrict::getParentId, cityId).orderByAsc(SysDistrict::getSort));
        return sysDistricts;
    }

    @Override
    public Object areaTree(Long provinceId) {
        //如果provinceId为空 则使用安徽省的id
        if (provinceId == null) {
            provinceId = 1346396167419748353L;
        }
        List<AreaTree> areaTrees = baseMapper.areaTree(provinceId);
        Map<Long, CityTreeVO> cityTreeVOMap = new HashMap<>();
        for (AreaTree areaTree : areaTrees) {
            if (cityTreeVOMap.get(areaTree.getCityId()) == null) {
                CityTreeVO cityTreeVO = new CityTreeVO();
                cityTreeVO.setCity(areaTree.getCity());
                cityTreeVO.setCityId(areaTree.getCityId());
                cityTreeVOMap.put(areaTree.getCityId(), cityTreeVO);
            }
            CityTreeVO cityTreeVO = cityTreeVOMap.get(areaTree.getCityId());
            AreaTreeVO areaTreeVO = new AreaTreeVO();
            areaTreeVO.setArea(areaTree.getArea());
            areaTreeVO.setAreaId(areaTree.getAreaId());
            cityTreeVO.getAreaTreeVOS().add(areaTreeVO);
        }
        List<CityTreeVO> cityTreeVOS = new ArrayList<>();
        //遍历  cityTreeVOMap
        for (Map.Entry<Long, CityTreeVO> entry : cityTreeVOMap.entrySet()) {
            cityTreeVOS.add(entry.getValue());
        }
        Map<String, Object> result = new HashMap<>();
        SysDistrict sysDistrict = baseMapper.selectById(provinceId);
        result.put("province", sysDistrict.getDistrictName());
        result.put("provinceId", sysDistrict.getDistrictId());
        result.put("tree", cityTreeVOS);
        return result;
    }


    /**
     * 获取sys_district
     *
     * @author 刘耀
     * @since 2021/03/15 20:13
     */
    private SysDistrict querySysDistrict(SysDistrictParam sysDistrictParam) {
        SysDistrict sysDistrict = this.getById(sysDistrictParam.getDistrictId());
        if (ObjectUtil.isEmpty(sysDistrict)) {
            throw new ServiceException(SysDistrictExceptionEnum.NOT_EXISTED);
        }
        return sysDistrict;
    }

    /**
     * 获取地区名称
     *
     * @return
     * @key 地区id
     * @value 名称
     */
    @Override
    public Map<Long, String> getMap() {
        HashMap<Long, String> map = new HashMap<>();

        List<SysDistrict> sysDistricts = baseMapper.selectList(new QueryWrapper<>(null));
        for (SysDistrict sysDistrict : sysDistricts) {
            map.put(sysDistrict.getDistrictId(), sysDistrict.getDistrictName());
        }


        return map;
    }
}